package top.lixunda.ecommerce.server.user.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import top.lixunda.commerce.common.core.entity.MessageObject;
import top.lixunda.ecommerce.server.business.BaseController;
import top.lixunda.ecommerce.server.business.api.entity.cache.CacheUser;
import top.lixunda.ecommerce.server.user.api.entity.db.UserAddressDO;
import top.lixunda.ecommerce.server.user.api.feign.IFeignUserAddressController;
import top.lixunda.ecommerce.server.user.service.IUserAddressService;

/**
 * 获取用户地址信息
 *
 * @author Xumda
 * @version time:2020/2/25 10:36
 */
@Slf4j
@RestController
public class FeignUserAddressController extends BaseController
        implements IFeignUserAddressController {

    private final IUserAddressService userAddressService;

    @Autowired
    public FeignUserAddressController(IUserAddressService userAddressService) {
        this.userAddressService = userAddressService;
    }

    /**
     * 根据id查询地址信息
     *
     * @param id 用户地址id
     * @return 用户地址，不存在返回null
     */
    @Override
    public MessageObject<UserAddressDO> queryAddressById(Long id) {
        if (id == null || id <= 0) {
            return MessageObject.error(UserAddressDO.class, "id不能为空且必须大于0");
        }
        CacheUser userDetail = currentUserCacheNotNull();
        UserAddressDO userAddress = userAddressService.getOne(
                new LambdaQueryWrapper<UserAddressDO>()
                        .eq(UserAddressDO::getId, id)
                        .eq(UserAddressDO::getAccount, userDetail.getAccount())
                        .eq(UserAddressDO::getUseable, UserAddressDO.USEABLE));
        return getSuccessMessageObject("获取地址成功", userAddress);
    }

}
